#include "Level.h"
#include "tinyxml2.h"

using namespace tinyxml2;
using namespace std;
using namespace sf;

string Level::levelText;
int Level::currentLevel = 1;
XMLDocument Level::doc;
map<string, string> Level::pathMap;


Level::Level(CharacterManager &characterManager) :
_characterManager(characterManager)
{
	
	doc.LoadFile("./config/config.xml");	
	XMLElement* xmlCurrentLevel = doc.FirstChildElement()->FirstChildElement("LEVELCONFIGURE")->FirstChildElement();	
	while (xmlCurrentLevel)
	{
		xmlCurrentLevel = xmlCurrentLevel->NextSiblingElement();
		numberLevel++;		
	}


	XMLElement* xmlPath = doc.FirstChildElement()->FirstChildElement("CONFIGUREPATH")->FirstChildElement();
		
	while (xmlPath)
	{
		string stringName = xmlPath->FirstChildElement("NAME")->GetText();
		string path = xmlPath->FirstChildElement("PATH")->GetText();
		pathMap[stringName] = path;
		xmlPath = xmlPath->NextSiblingElement();
		
	}


}

int Level::getCurrentLevel()
{
	return currentLevel;
}

int Level::getNumberLevel()
{
	return numberLevel;
}

string Level::getLevelText()
{
	return levelText;
}

string Level::getPath(SpriteEnum sprite)
{	
	return pathMap[spriteEnumToString[sprite]];
}

string Level::getMusicPath(MusicEnum music)
{
	
	return pathMap[musicEnumToString[music]];
}


void Level::setLevel(int x)
{
	currentLevel = x;
}

std::map<int, Character*> &Level::getCharacterMap()
{
	return _characterManager.getCharacterMap();
}

void Level::configure()
{
	
	parseXML();  
	
	_characterManager.addCharacter(Vector2f(20, 300), float(healthVector[0]), sizeVector[0], "player", spriteVector[0], ROCKET1);
	

	for (int i = 1; i <numberEnemy; i++)
	{
		_characterManager.addCharacter(Vector2f(0, 0), float(healthVector[i]), sizeVector[i], "enemy", spriteVector[i], ENEMYBULLET1);
	}
	
	
	
}

void Level::parseXML()
{
		numberEnemy = 0;
		sizeVector.clear();
		healthVector.clear();		
		spriteVector.clear();

	
		
		string level ="LEVEL"+to_string(currentLevel);
		const char * levelChar = level.c_str(); //convertion string en const char*
		

	
		XMLElement* xmlCurrentEnnemy = doc.FirstChildElement()->FirstChildElement()->FirstChildElement(levelChar)->FirstChildElement("characters")->FirstChildElement(); // think problem is there		
		
		while (xmlCurrentEnnemy)
		{
			string hp=xmlCurrentEnnemy->FirstChildElement("HP")->GetText();	
			size = xmlToVector2f(XMLMap[xmlCurrentEnnemy->FirstChildElement("SIZE")->GetText()]);
			characterSprite = stringToSpriteEnum[xmlCurrentEnnemy->FirstChildElement("SPRITE")->GetText()];

			spriteVector.push_back(characterSprite);
			sizeVector.push_back(size);			
			healthVector.push_back(stoi(hp));
			xmlCurrentEnnemy = xmlCurrentEnnemy->NextSiblingElement(); 
			numberEnemy++;

		}

		
		levelText = doc.FirstChildElement()->FirstChildElement()->FirstChildElement(levelChar)->FirstChildElement("levelText")->GetText();
		
}

Vector2f Level::xmlToVector2f(XMLEnum word)
{
	switch (word)
	{
	case SMALL:
	{
		return Vector2f(50, 50);
	break;
	}
		
	case MEDIUM:
	{
		return Vector2f(80, 80);
	break;
	}
		
	case BIG:
	{
		return Vector2f(150, 150);
	break;
	}
		
	case ENORMOUS:
	{
		return Vector2f(250, 250);
	break;
	}
		
	default:
	{
		return Vector2f(0, 0);
	break;
	}

	
	}

}